﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;
using System.Drawing;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    class Spell_LizardSkin : Buff
    {
        private int ArmorAdded = 0;

        public Spell_LizardSkin()
        {
            minutesLeft = 10;
        }

        public override bool castWhileAlone(SpellIcon sIcon)
        {
            return true;
        }

        private CreatureBase choosenTarget;
        public override bool hasCustomTarget(SpellIcon sIcon)
        {
            return true;
        }

        public override bool hasValidTarget(SpellIcon sIcon, Creature creature, List<Point> hiddenList)
        {
            if (creature.buffManager.GetBuffByStackType(sIcon.StackType) == null)
            {
                choosenTarget = creature;
                return true;
            }

            List<CreatureBase> targetCreatures = new List<CreatureBase>();
            foreach (MapItem mapitem in creature.currentMapItem.mapItemsWithin4)
            {
                if (CreatureAI.isHidden(mapitem.MapX, mapitem.MapY, hiddenList) == false)
                {
                    foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                    {
                        if (creatureBase.Faction == creature.Faction && creatureBase.buffManager.GetBuffByStackType(sIcon.StackType) == null)
                            targetCreatures.Add(creatureBase);
                    }
                }
            }

            if (targetCreatures.Count > 0)
            {
                int indexToChoose = Managers.GameHandler.random.Next(0, targetCreatures.Count);
                choosenTarget = targetCreatures[indexToChoose];
            }

            if (choosenTarget == null)
                return false;
            else
                return true;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            ArmorAdded = SkillManager.AdjustForCombat(System.Convert.ToInt32(castLevel / 1.5), 0, CastingCreatureWis / 2);

            TargetCreatureBase.Armor = TargetCreatureBase.Armor + ArmorAdded;
            TargetCreatureBase.Update_Character();

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            //If its a creature custom cast, replace the targets ID
            if (choosenTarget != null)
                CreatureGUID = choosenTarget.CreatureID;

            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            ArmorAdded = SkillManager.AdjustForCombat(System.Convert.ToInt32(CastLevel / 1.5), 0, CastingCreatureWis / 2);
            minutesLeft = System.Convert.ToInt32(Math.Floor(CastingCreatureWis / 2.00));

            TargetCreatureBase.Armor = TargetCreatureBase.Armor + ArmorAdded;

            TargetCreatureBase.Update_Character();
            TargetCreatureBase.buffManager.UpdateBuffs();

            //Start Spell Timer
            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);
            TargetCreatureBase.Armor = TargetCreatureBase.Armor - ArmorAdded;
            TargetCreatureBase.Update_Character();
        }
    }
}
